<?php
namespace app\common\controller;
use Process\Process;
use Exception;
/**
 * Class Mobile
 * @package app\common\controller
 */
class Mobile
{
    public  $run_param;

    public $process_arr ;

    public function __construct($run_param )
    {
        $this->run_param = $run_param;
    }

    /**
     * @throws Exception
     */
    public function start()
    {
        if($this->run_param)
        {
            #设置pid文件
            $pid_path = dirname($this->run_param['pid_path']).'/master.pid';
            if(file_exists($pid_path))
            {
                $pid = trim(file_get_contents($pid_path));
                if($pid)
                {
                    if(posix_kill($pid, 0))
                    {
                        throw new Exception("已有服务已运行!pid:{$pid}".PHP_EOL);
                    }
                }
            }
            if($this->makeFile($pid_path, getmypid()))
            {
                $this->process_arr = explode(':', $this->run_param['process']);
                if($this->process_arr[0]<=0||$this->process_arr[1] <= 0 )
                {
                    throw new Exception('进程设置必须大于0');
                }
                $work_obj = new SmsWork();
                $email_work_obj = new EmailWork();
                # 指定任务
                $run_call_backs = array_merge(
                    array_fill(0,$this->process_arr[0],array($work_obj,'queueWork')) ,
                    array_fill(0,$this->process_arr[0],array($email_work_obj,'queueWork'))
                );
                $run_call_backs[] = array($work_obj ,'dbSaveHy');
                $run_call_backs[] = array($work_obj ,'dbSaveYx');
                $run_call_backs[] = array($email_work_obj ,'dbSave');
                #配置更新进程
                $run_call_backs[] = array($work_obj ,'confReady');
                $process = new Process(count($run_call_backs), 'mobile ',  $run_call_backs, 1 , $this->run_param['memory'],$this->run_param['pid_path']);
                echo 'mobile service started !'.PHP_EOL;
                $process->run(1,$this->run_param['sleep']) ;
            }
            else
            {
                throw new Exception('pid  文件创建失败!'.PHP_EOL);
            }
        }
        else
        {
            throw new Exception('启动参数未设置！'.PHP_EOL);
        }
    }

    /**
     * @throws \Exception
     */
    public function reload()
    {
        if($this->run_param)
        {
            $this->stop();
            $this->start();
        }
        else
        {
            throw new Exception('启动参数未设置！'.PHP_EOL);
        }
    }

    /**
     * @throws \Exception
     */
    public function stop()
    {
        if($this->run_param)
        {
            $pid_path = dirname($this->run_param['pid_path']).'/master.pid';
            if(is_file($pid_path))
            {
                $pid = file_get_contents($pid_path);
                if(posix_kill($pid, SIGINT))
                {
                    do{
                        sleep(1);
                    }
                    while(posix_kill($pid, 0));
                    unlink($pid_path);
                    echo 'mobile service is stoped!'.PHP_EOL;
                }
            }
            else
            {
                throw new Exception("can not open {$pid_path}".PHP_EOL);
            }
        }
        else
        {
            throw new Exception('启动参数未设置！'.PHP_EOL);
        }
    }

    /**
     * 给定一文件路径自动创建目录和文件
     * @param $path
     * @param $data
     * @return bool
     */
    protected function makeFile($path,$data)
    {
        if(!is_dir(dirname($path)))
        {
            mkdir(dirname($path),0777,true);
        }
        if(file_put_contents($path, $data)===false)
        {
            return false ;
        }
        else{
            return true ;
        }
    }
}



